!Traitement de données: f_{\mu_1}/f_{\mu_2} pour les différents temps.
!Permet de savoir si le a_2 de relaxation dépend de \mu.
!Mettre dans le même répertoire que le programme les répertoires contenant les fichiers originaux.
!Ensuite on peut effacer ces répertoires, et lancer le fichier GnuPlot.

program a2apdata
implicit none
double precision, parameter :: xmin = -2.5d0 !intervalle min du plot
double precision, parameter :: xmax = +2.5d0 !intervalle max du plot
double precision, dimension(1:1000,1:2) :: da1,da2,da3,da4,da5,da6,da7,da8,da9,da10
double precision, dimension(1:1000,1:2) :: db1,db2,db3,db4,db5,db6,db7,db8,db9,db10
double precision, dimension(1:1000,1:2) :: out1,out2,out3,out4,out5,out6,out7,out8,out9,out10
integer, dimension(1:10) :: dima,dimb !nombre de points dans l'intervalle [xmin,xmax]
integer :: i,j,io

!====================
!Lecture des fichiers
!====================

open(unit=1,file="20M05p0mu/a2ap-2-1h.txt")
io = 1
i = 1
lecturea1: do
	read(1,*,iostat=io) da1(i,1),da1(i,2)
	if ((da1(i,1)) > xmax .or. (io<0)) exit lecturea1
	if (da1(i,1) >= xmin) i=i+1
end do lecturea1
close(1)
dima(1) = i-1

open(unit=1,file="20M05p0mu/a2ap-2-2h.txt")
io = 1
i = 1
lecturea2: do
	read(1,*,iostat=io) da2(i,1),da2(i,2)
	if ((da2(i,1)) > xmax .or. (io<0)) exit lecturea2
	if (da2(i,1) >= xmin) i=i+1
end do lecturea2
close(1)
dima(2) = i-1

open(unit=1,file="20M05p0mu/a2ap-2-3h.txt")
io = 1
i = 1
lecturea3: do
	read(1,*,iostat=io) da3(i,1),da3(i,2)
	if ((da3(i,1)) > xmax .or. (io<0)) exit lecturea3
	if (da3(i,1) >= xmin) i=i+1
end do lecturea3
close(1)
dima(3) = i-1

open(unit=1,file="20M05p0mu/a2ap-2-4h.txt")
io = 1
i = 1
lecturea4: do
	read(1,*,iostat=io) da4(i,1),da4(i,2)
	if ((da4(i,1)) > xmax .or. (io<0)) exit lecturea4
	if (da4(i,1) >= xmin) i=i+1
end do lecturea4
close(1)
dima(4) = i-1

open(unit=1,file="20M05p0mu/a2ap-2-5h.txt")
io = 1
i = 1
lecturea5: do
	read(1,*,iostat=io) da5(i,1),da5(i,2)
	if ((da5(i,1)) > xmax .or. (io<0)) exit lecturea5
	if (da5(i,1) >= xmin) i=i+1
end do lecturea5
close(1)
dima(5) = i-1

open(unit=1,file="20M05p0mu/a2ap-2-6h.txt")
io = 1
i = 1
lecturea6: do
	read(1,*,iostat=io) da6(i,1),da6(i,2)
	if ((da6(i,1)) > xmax .or. (io<0)) exit lecturea6
	if (da6(i,1) >= xmin) i=i+1
end do lecturea6
close(1)
dima(6) = i-1

open(unit=1,file="20M05p0mu/a2ap-2-7h.txt")
io = 1
i = 1
lecturea7: do
	read(1,*,iostat=io) da7(i,1),da7(i,2)
	if ((da7(i,1)) > xmax .or. (io<0)) exit lecturea7
	if (da7(i,1) >= xmin) i=i+1
end do lecturea7
close(1)
dima(7) = i-1

open(unit=1,file="20M05p0mu/a2ap-2-8h.txt")
io = 1
i = 1
lecturea8: do
	read(1,*,iostat=io) da8(i,1),da8(i,2)
	if ((da8(i,1)) > xmax .or. (io<0)) exit lecturea8
	if (da8(i,1) >= xmin) i=i+1
end do lecturea8
close(1)
dima(8) = i-1

open(unit=1,file="20M05p0mu/a2ap-2-9h.txt")
io = 1
i = 1
lecturea9: do
	read(1,*,iostat=io) da9(i,1),da9(i,2)
	if ((da9(i,1)) > xmax .or. (io<0)) exit lecturea9
	if (da9(i,1) >= xmin) i=i+1
end do lecturea9
close(1)
dima(9) = i-1

open(unit=1,file="20M05p0mu/a2ap-2-10h.txt")
io = 1
i = 1
lecturea10: do
	read(1,*,iostat=io) da10(i,1),da10(i,2)
	if ((da10(i,1)) > xmax .or. (io<0)) exit lecturea10
	if (da10(i,1) >= xmin) i=i+1
end do lecturea10
close(1)
dima(10) = i-1



open(unit=1,file="20M05p3mu/a2ap-2-1h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-1h.txt")
io = 1
i = 1
lectureb1: do
	read(1,*,iostat=io) db1(i,1),db1(i,2)
	if ((db1(i,1)) > xmax .or. (io<0)) exit lectureb1
	if (db1(i,1) >= xmin) i=i+1
end do lectureb1
close(1)
dimb(1) = i-1

open(unit=1,file="20M05p3mu/a2ap-2-2h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-2h.txt")
io = 1
i = 1
lectureb2: do
	read(1,*,iostat=io) db2(i,1),db2(i,2)
	if ((db2(i,1)) > xmax .or. (io<0)) exit lectureb2
	if (db2(i,1) >= xmin) i=i+1
end do lectureb2
close(1)
dimb(2) = i-1

open(unit=1,file="20M05p3mu/a2ap-2-3h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-3h.txt")
io = 1
i = 1
lectureb3: do
	read(1,*,iostat=io) db3(i,1),db3(i,2)
	if ((db3(i,1)) > xmax .or. (io<0)) exit lectureb3
	if (db3(i,1) >= xmin) i=i+1
end do lectureb3
close(1)
dimb(3) = i-1

open(unit=1,file="20M05p3mu/a2ap-2-4h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-4h.txt")
io = 1
i = 1
lectureb4: do
	read(1,*,iostat=io) db4(i,1),db4(i,2)
	if ((db4(i,1)) > xmax .or. (io<0)) exit lectureb4
	if (db4(i,1) >= xmin) i=i+1
end do lectureb4
close(1)
dimb(4) = i-1

open(unit=1,file="20M05p3mu/a2ap-2-5h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-5h.txt")
io = 1
i = 1
lectureb5: do
	read(1,*,iostat=io) db5(i,1),db5(i,2)
	if ((db5(i,1)) > xmax .or. (io<0)) exit lectureb5
	if (db5(i,1) >= xmin) i=i+1
end do lectureb5
close(1)
dimb(5) = i-1

open(unit=1,file="20M05p3mu/a2ap-2-6h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-6h.txt")
io = 1
i = 1
lectureb6: do
	read(1,*,iostat=io) db6(i,1),db6(i,2)
	if ((db6(i,1)) > xmax .or. (io<0)) exit lectureb6
	if (db6(i,1) >= xmin) i=i+1
end do lectureb6
close(1)
dimb(6) = i-1

open(unit=1,file="20M05p3mu/a2ap-2-7h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-7h.txt")
io = 1
i = 1
lectureb7: do
	read(1,*,iostat=io) db7(i,1),db7(i,2)
	if ((db7(i,1)) > xmax .or. (io<0)) exit lectureb7
	if (db7(i,1) >= xmin) i=i+1
end do lectureb7
close(1)
dimb(7) = i-1

open(unit=1,file="20M05p3mu/a2ap-2-8h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-8h.txt")
io = 1
i = 1
lectureb8: do
	read(1,*,iostat=io) db8(i,1),db8(i,2)
	if ((db8(i,1)) > xmax .or. (io<0)) exit lectureb8
	if (db8(i,1) >= xmin) i=i+1
end do lectureb8
close(1)
dimb(8) = i-1

open(unit=1,file="20M05p3mu/a2ap-2-9h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-9h.txt")
io = 1
i = 1
lectureb9: do
	read(1,*,iostat=io) db9(i,1),db9(i,2)
	if ((db9(i,1)) > xmax .or. (io<0)) exit lectureb9
	if (db9(i,1) >= xmin) i=i+1
end do lectureb9
close(1)
dimb(9) = i-1

open(unit=1,file="20M05p3mu/a2ap-2-10h.txt")
!open(unit=1,file="40M05pm1.5mu/a2ap-2-10h.txt")
io = 1
i = 1
lectureb10: do
	read(1,*,iostat=io) db10(i,1),db10(i,2)
	if ((db10(i,1)) > xmax .or. (io<0)) exit lectureb10
	if (db10(i,1) >= xmin) i=i+1
end do lectureb10
close(1)
dimb(10) = i-1

!===============================================
!Création du nouveau fichier f_{\mu_1}/f_{\mu_2}
!===============================================

if (dima(1) == dimb(1)) then
	do i=1,dima(1)
		out1(i,1) = da1(i,1)
		out1(i,2) = da1(i,2)/db1(i,2)
	end do
else
	out1(:,2) = 2.d0
	if (dima(1) > dimb(1)) out1(:,1) = da1(:,1)
	if (dima(1) < dimb(1)) out1(:,1) = db1(:,1)
end if

if (dima(2) == dimb(2)) then
	do i=1,dima(2)
		out2(i,1) = da2(i,1)
		out2(i,2) = da2(i,2)/db2(i,2)
	end do
end if

if (dima(3) == dimb(3)) then
	do i=1,dima(3)
		out3(i,1) = da3(i,1)
		out3(i,2) = da3(i,2)/db3(i,2)
	end do
end if

if (dima(4) == dimb(4)) then
	do i=1,dima(4)
		out4(i,1) = da4(i,1)
		out4(i,2) = da4(i,2)/db4(i,2)
	end do
end if

if (dima(5) == dimb(5)) then
	do i=1,dima(5)
		out5(i,1) = da5(i,1)
		out5(i,2) = da5(i,2)/db5(i,2)
	end do
end if

if (dima(6) == dimb(6)) then
	do i=1,dima(6)
		out6(i,1) = da6(i,1)
		out6(i,2) = da6(i,2)/db6(i,2)
	end do
end if

if (dima(7) == dimb(7)) then
	do i=1,dima(7)
		out7(i,1) = da7(i,1)
		out7(i,2) = da7(i,2)/db7(i,2)
	end do
end if

if (dima(8) == dimb(8)) then
	do i=1,dima(8)
		out8(i,1) = da8(i,1)
		out8(i,2) = da8(i,2)/db8(i,2)
	end do
end if

if (dima(9) == dimb(9)) then
	do i=1,dima(9)
		out9(i,1) = da9(i,1)
		out9(i,2) = da9(i,2)/db9(i,2)
	end do
end if

if (dima(10) == dimb(10)) then
	do i=1,dima(10)
		out10(i,1) = da10(i,1)
		out10(i,2) = da10(i,2)/db10(i,2)
	end do
end if


!=====================
!Sauvegarde du fichier
!=====================

open(unit=1,file="a2ap-2-1data.txt")
	do i=1,dima(1)
		write(1,*) out1(i,1),out1(i,2)
	end do
close(1)

open(unit=1,file="a2ap-2-2data.txt")
	do i=1,dima(2)
		write(1,*) out2(i,1),out2(i,2)
	end do
close(1)

open(unit=1,file="a2ap-2-3data.txt")
	do i=1,dima(3)
		write(1,*) out3(i,1),out3(i,2)
	end do
close(1)

open(unit=1,file="a2ap-2-4data.txt")
	do i=1,dima(4)
		write(1,*) out4(i,1),out4(i,2)
	end do
close(1)

open(unit=1,file="a2ap-2-5data.txt")
	do i=1,dima(5)
		write(1,*) out5(i,1),out5(i,2)
	end do
close(1)

open(unit=1,file="a2ap-2-6data.txt")
	do i=1,dima(6)
		write(1,*) out6(i,1),out6(i,2)
	end do
close(1)

open(unit=1,file="a2ap-2-7data.txt")
	do i=1,dima(7)
		write(1,*) out7(i,1),out7(i,2)
	end do
close(1)

open(unit=1,file="a2ap-2-8data.txt")
	do i=1,dima(8)
		write(1,*) out8(i,1),out8(i,2)
	end do
close(1)

open(unit=1,file="a2ap-2-9data.txt")
	do i=1,dima(9)
		write(1,*) out9(i,1),out9(i,2)
	end do
close(1)

open(unit=1,file="a2ap-2-10data.txt")
	do i=1,dima(10)
		write(1,*) out10(i,1),out10(i,2)
	end do
close(1)

write(*,*)
write(*,*) "Programme termine."
read(*,*) 
end program a2apdata